﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Guru.Web.Controls.Telerik;
using Guru.Web.UI;
using System.Collections.ObjectModel;
using Floor = UserControl_FloorDiagram;
using Room = UserControl_Room;
using Col = Column<string, System.Type, int>;
using System.Data;
using System.Collections;
using Guru.Data;

[PageTextResource("UserControl_HotelDiagram")]
public partial class UserControl_HotelDiagram : GuruUserControl
{   
    #region Properties
    public DataTable RoomList
    {
        get;
        set;
    }
    public bool HideRoomStatus
    {
        set;
        get;
    }
    public string HeaderText
    {
        set;
        get;
    }
    public HSMEnums.SelectionMode SelectionMode
    {
        set;
        get;
    }
    public HSMEnums.SelectionMode FloorSelectionMode
    {
        set;
        get;
    }
    public List<string> SelectedRooms
    {
        get
        {
            List<string> selectedRooms = new List<string>();
            foreach (RepeaterItem item in uxFloorList.Items)
            {
                Floor floor = item.FindControl("uxFloor") as Floor;
                if (floor != null && floor.SelectedRooms.Count > 0)
                {
                    selectedRooms.AddRange(floor.SelectedRooms);
                }
            }
            return selectedRooms;
        }
    }
    #endregion

    protected void uxFloorList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                DataRowView dataView = (DataRowView)e.Item.DataItem;
                Floor floor = (Floor)e.Item.FindControl("uxFloor");
                floor.FloorName = dataView["FloorName"].ToString();
                floor.RoomSelectionMode = this.SelectionMode;
                floor.Rooms = this.GetRoomsByFloor(int.Parse(dataView["FloorID"].ToString()));
                floor.DataBind();
                break;
        }
    }
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        this.uxRoomStatus.Visible = !this.HideRoomStatus;
        if (!string.IsNullOrEmpty(this.HeaderText))
        {
            this.uxContainerHotelDia.HeaderText = this.HeaderText;
        }
    }

    protected DataTable GetFloors()
    {
        if (this.RoomList == null) return null;
        DataTable floors = new DataTable();
        floors.Columns.Add("FloorID", typeof(string));
        floors.Columns.Add("FloorName", typeof(string));
        for (int i = 0; i < this.RoomList.Rows.Count;i++ )
        {
            bool isExists = false;
            for (int j = 0; j < floors.Rows.Count; j++)
            {
                if (floors.Rows[j]["FloorID"].ToString().Equals(this.RoomList.Rows[i]["FloorID"].ToString()))
                {
                    isExists = true;
                    break;
                }
            }
            if (isExists == false)
            {
                DataRow flRow = floors.NewRow();
                flRow["FloorName"] = this.RoomList.Rows[i]["FloorName"].ToString();
                flRow["FloorID"] = this.RoomList.Rows[i]["FloorID"].ToString();
                floors.Rows.Add(flRow);
            }
        }
        return floors;
    }   
    protected DataTable GetRoomsByFloor(int floorID)
    {
        if (this.RoomList == null) return null;
        var results = from myRow in this.RoomList.AsEnumerable()
                      where myRow.Field<int>("FloorID") == floorID && myRow.Field<object>("RoomID").IsNotNull()
                      select myRow;
        if (results == null || results.Count() <= 0) return null;
        return results.CopyToDataTable();

    }

    public void DataBind()
    {        
        this.uxFloorList.DataSource = this.GetFloors();
        this.uxFloorList.DataBind();
        this.ShowRoomStatus();
    }

    protected void ShowRoomStatus()
    {
        this.uxCountEmpty.Text = this.CountByRoomStatus(HSMEnums.RoomStatus.Empty).ToString();
        this.uxCountCheckedIn.Text = (this.CountByRoomStatus(HSMEnums.RoomStatus.SingleCheckedIn) + this.CountByRoomStatus(HSMEnums.RoomStatus.MultiCheckedIn)).ToString();
        this.uxCountBooked.Text = this.CountByRoomStatus(HSMEnums.RoomStatus.Reserved).ToString();
        this.uxCountDirty.Text = this.CountByRoomStatus(HSMEnums.RoomStatus.Dirty).ToString();
        this.uxCountMaintance.Text = this.CountByRoomStatus(HSMEnums.RoomStatus.Maintenance).ToString();
    }
    protected int CountByRoomStatus(HSMEnums.RoomStatus status)
    {
        if (this.RoomList == null) return 0;
        var rooms = from myRow in this.RoomList.AsEnumerable()
                    where myRow.Field<int>("Status") == (int)status
                         select myRow;
        return rooms.Count();
    }
}
